/**
 * <p>
 * 版权所有：中企动力
 * </p>
 * <p>
 * 未经本公司许可，不得以任何方式复制或使用本程序任何部分
 * </p>
 */
package com.ce.pms.renovation.web;

import java.util.ArrayList;
import java.util.List;

import com.ce.pms.base.web.PmsBaseAction;
import com.ce.pms.renovation.service.IInspectionService;
import com.ce.pms.renovation.vo.InspectionViolationVO;
import com.ce.pms.renovation.vo.ReinspectionVO;
import com.ce.util.BlankUtil;
import com.ce.util.DateUtil;
import com.sitechasia.webx.core.annotation.Out;
import com.sitechasia.webx.core.annotation.Read;

/**
 * 复验Action
 *
 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
 * @Created 2008-12-26
 */

public class ReinspectionAction extends PmsBaseAction {

	private final String		Edit_PAGE	= "reinspectionEdit";

	/**
	 * 装修验收Service
	 */
	private IInspectionService	inspectionService;

	/**
	 * 初始页面
	 */
	private final String		LIST_PAGE	= "reinspectionList";

	/**
	 * 添加页面
	 *
	 * @return
	 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
	 * @created 2008-12-29
	 */
	public String addPage() {
		return this.Edit_PAGE;
	}

	/**
	 * 删除复验
	 *
	 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
	 * @created 2008-12-26
	 */
	public void deleteReinspection(@Read(key = "keyword") Long keyword) {
		try {
			this.inspectionService.deleteReinspection(keyword);
		} catch (RuntimeException re) {
			throw re;
		}
		this.renderText("true");
	}

	/**
	 * 编辑页面
	 *
	 * @param keyword 主键
	 * @return
	 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
	 * @created 2008-12-29
	 */
	public String editPage(
			@Read(key = "keyword") @Out(key = "reinspectionId") Long reinspectionId) {
		this.inspectionService.getReinspection(reinspectionId);
		return this.Edit_PAGE;
	}

	/**
	 * 编辑复验
	 *
	 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
	 * @created 2008-12-26
	 */
	public void editReinspection(@Read ReinspectionVO reinspectionVO) {
		if(!BlankUtil.isBlank(reinspectionVO.getInspectionDateStr())) {
			reinspectionVO.setInspectionDate(DateUtil.convertStrToDate(reinspectionVO.getInspectionDateStr(), "yyyy-MM-dd HH时"));
		}

		try {

			if (reinspectionVO.getKeyword() == null) {// 添加
				reinspectionVO.setCreator(getUserId());
				reinspectionVO = this.inspectionService
						.addReinspection(reinspectionVO);
			} else {// 编辑
				reinspectionVO.setModifier(this.getUserId());
				reinspectionVO = this.inspectionService
						.editReinspection(reinspectionVO);
			}
			if(reinspectionVO==null) {
				throw new RuntimeException("保存失败");
			}
			this.renderText(reinspectionVO.getKeyword().toString());
		} catch (RuntimeException re) {
			throw re;
		}
	}

	/**
	 * 查询复验列表
	 *
	 * @return
	 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
	 * @created 2008-12-26
	 */
	public String queryReinspectionList(
			@Read(key = "inspectionRequisitionId") @Out(key = "inspectionRequisitionId") Long inspectionRequisitionId) {
		List<ReinspectionVO> tmpList = this.inspectionService
				.getReinspections(inspectionRequisitionId);
		List<ReinspectionVO> list = new ArrayList<ReinspectionVO>();
		for (ReinspectionVO tmpVO : tmpList) {
			if ("001".equals(tmpVO.getResult())) {
				tmpVO.setResult("通过");
			} else {
				tmpVO.setResult("不通过");
			}

			list.add(tmpVO);
		}

		this.setAttribute("queryDataList", list);

		this.setAttribute("totalRows", list.size());
		return this.LIST_PAGE;
	}

	/**
	 * @param inspectionService the inspectionService to set
	 */
	public void setInspectionService(IInspectionService inspectionService) {
		this.inspectionService = inspectionService;
	}
}
